package tbs.framework.sql.logger.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import tbs.framework.log.ILogger;
import tbs.framework.log.annotations.AutoLogger;
import tbs.framework.sql.config.SqlConfig;
import tbs.framework.sql.logger.ISqlLogger;
import tbs.framework.sql.model.SqlRuntimeStatus;

import java.util.Map;

/**
 * @author abstergo
 */
public class SimpleJsonLogger implements ISqlLogger {

    public SimpleJsonLogger() {
    }

    @AutoLogger(factory = SqlConfig.SQL_LOG_FACTORY_BEAN_NAME)
    private ILogger logger;

    @Override
    public void log(SqlRuntimeStatus sqlRuntimeStatus) {
        if (logger == null) {
            return;
        }
        String logMessage = JSON.toJSONString(
            Map.of("Execute SQL", sqlRuntimeStatus.getPrepareSql(), "cost", sqlRuntimeStatus.getExecuteTime() + "ms",
                "params", sqlRuntimeStatus.getParameterObject()), JSONWriter.Feature.PrettyFormat);
        logger.info(logMessage);
    }
}
